#! /usr/bin/env python

# This program is used to automatically fill in a literature reading report

# problems:
# 1. the version of chrome and chromedriver is different
# solve: download the corresponding version of chromedriver
# 2. can not find the target element on the web site
# solve: We need to enter the absolute path of the target element rather than the relative path
# The odd thing is that absolute xpath work on Yahoo sites, but relative xpath are required in IEEE website
# 3. chromedriver can't be found in the .py code
# solve: the chromedriver.exe need to be put on the python install path 

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

browser = webdriver.Chrome()
type(browser)
# browser.get('https://www.baidu.com')    # open baidu in chrome
browser.get('https://ieeexplore.ieee.org/document/8468986')    # open IEEE in chrome
assert 'IEEE' in browser.title
# cite_this = browser.find_element_by_class_name("layout-btn-white cite-this-btn hide-desktop")
# cite_this = browser.find_element_by_xpath("//div[@name='Cite This'][@type='button']")
# cite_this = browser.find_element_by_xpath("/html[1]/body[1]/div[7]/div[1]/div[1]/div[1]/div[3]/div[1]/xpl-root[1]/div[1]/xpl-document-details[1]/div[1]/div[1]/section[2]/div[1]/xpl-document-header[1]/section[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/xpl-cite-this-modal[1]/div[1]/button[1]")
cite_this = browser.find_element_by_xpath("//xpl-document-header/section[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/xpl-cite-this-modal[1]/div[1]/button[1]")
ActionChains(browser).click(cite_this).perform()
# browser.switch_to_window()
bibtex_but = browser.find_element_by_xpath("//a[contains(text(),'BibTeX')]")
ActionChains(browser).click(bibtex_but).perform()
time.sleep(5)   #sleep delay is not well
cita_and_abstract = browser.find_element_by_xpath("/html[1]/body[1]/ngb-modal-window[1]/div[1]/div[1]/div[1]/div[4]/div[1]/div[2]/input[1]")
ActionChains(browser).click(cita_and_abstract).perform()
# copy_but = browser.find_element_by_xpath("/html[1]/body[1]/ngb-modal-window[1]/div[1]/div[1]/div[1]/div[4]/div[1]/div[1]/div[1]")
# ActionChains(browser).click(copy_but).perform()
# copy2clip_but = browser.find_elements_by_css_selector("body.body-resp.cmpl_embed_complete.modal-open:nth-child(2) ngb-modal-window.modal.fade.show.d-block.authors-disambiguity-container:nth-child(21) div.modal-dialog div.modal-content div.cite-this-container div:nth-child(4) div.btn-wrapper > button.layout-btn-blue.stats-Cite_This_Doc_Details_Copy")
# ActionChains(browser).double_click(copy2clip_but).perform()
time.sleep(5)
cite_text = browser.find_element_by_xpath("/html[1]/body[1]/ngb-modal-window[1]/div[1]/div[1]/div[1]/div[4]/pre[1]").text
# cite_text = browser.find_element_by_xpath("//pre[contains(text(),'author={E. {Khorov} and A. {K')]").text
# print(cite_text)

f1 = open(r'F:\mypythonfile\python_Daily_practice\small_items\article.bib','w')   #these path need change when you use this program
print(cite_text, file=f1)
f1.close()
browser.quit()






# ***************************************next code is useless***************************** #
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys

# browser = webdriver.Chrome()
# browser.get('http://www.yahoo.com')
# assert 'Yahoo' in browser.title

# elem = browser.find_element_by_xpath("/html[1]/body[1]/header[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[2]/form[1]/input[1]")  # Find the search box
# # elem = browser.find_element_by_xpath("//input[@id='ybar-sbq']")  # Find the search box
# elem.send_keys('seleniumhq' + Keys.RETURN)  # write sth in the search box

# # browser.quit()